<template>
  <div class="autoDetail">
    <h1 class="back-title"><i @click="$router.go(-1)" class="iconfont icon-fanhui"></i>{{!id ?'添加新的':'编辑'}}产品模板</h1>
    <el-form ref="form" :model="form" label-width="90px" label-position="left" :rules="rules">
      <el-form-item label="模块名称:" prop="templateName">
        <el-input v-model="form.templateName" placeholder="请输入模块名称" size="small" maxlength="20"
                  style="width:260px"></el-input>
      </el-form-item>
      <el-form-item label="是否共享:" prop="isShare">
        <el-radio v-model="form.isShare" :label="true">是</el-radio>
        <el-radio v-model="form.isShare" :label="false">否</el-radio>
      </el-form-item>
      <el-form-item label="模板详情:" prop="htmlContent">
        <!-- 文本编辑器-->
        <ProductRight ref="childEdit" @getInfoMake="getInfoMake" :htmlContent="form.htmlContent"></ProductRight>
      </el-form-item>
      <el-form-item class="common-btn">
        <el-button size="small" @click="$router.go(-1)">取 消</el-button>
        <el-button  size="small" type="primary" @click="onSubmit('form')"  :loading="loading">{{loading?'保存中':'确 定'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    import {saveProduct, productTemplateDetail, editProduct} from '@/api/automation/product'
    import ProductRight from '@/components/automation/template/ProductRight'

    export default {
        name: "addMail",
        components: {ProductRight},
        data() {
            return {
                loading:false,
                form: {
                    templateName: '',
                    htmlContent: '',
                    isShare: false
                },
                rules: {
                    templateName: [
                        {required: true, message: '请输入产品模板名称', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                    htmlContent: [
                        {required: true, message: '请输入产品模板详情', trigger: 'blur'}
                    ],
                    isShare: [
                        {required: true, message: '请选择是否共享', trigger: 'blur'},
                    ]
                },
                id:''
            }
        },

        mounted() {
            this.$refs.childEdit.init();
            if(this.$route.path.indexOf('add')==-1){
                this.id= this.$route.params.id;
                this.getData();
            }
        },
        methods: {
            getData() {
                productTemplateDetail(this.$route.params.id).then(res => {
                    if (res.data.success) {
                        this.form = res.data.data;
                        this.$refs.childEdit.init(this.form.htmlContent);//初始化
                    }
                })
            },
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading=true;
                        if (!this.id) {//新增
                            saveProduct(this.form).then(res => {
                                this.dealEnd(res);
                            }).catch(()=>this.loading=false)
                        } else {//编辑
                            editProduct(this.id, this.form).then(res => {
                                this.dealEnd(res);
                            }).catch(()=>this.loading=false)
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res) {
                this.loading=false;
                if (res.data.success) {
                    this.$message({
                        type: 'success',
                        message: res.data.msg
                    });
                    this.$router.go(-1)
                } else {
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }
            },
            getInfoMake(content) {//文本编辑器数据
                this.form.htmlContent = encodeURIComponent(content.emailContent);
            },
        }
    }
</script>

<style lang="scss" scoped>
  .autoDetail {
    .my-edit {
      overflow: hidden;
      border-bottom: 1px solid #ddd;
    }
  }

</style>
